package learnonline.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import learnonline.dao.RowMapper;
import learnonline.dao.TeacherDao;
import learnonline.entity.Teacher;

public class TeacherDaoImpl implements	TeacherDao{
	private JdbcImpl jdbc=new JdbcImpl();
	private static class TeacherRowMapper implements RowMapper<Teacher>{

		@Override
		public Teacher mapRow(ResultSet rs) throws SQLException {
			Teacher c=new Teacher();
			c.setTid(rs.getInt("tid"));	
			c.setTname(rs.getString("tname"));
			c.setTdescription(rs.getString("tdescription"));
			c.setTimage(rs.getString("timage"));
			return c;
		}
		
	}
	@Override
	public List<Teacher> getAllTeacher() {
		String sql="select * from teacher";
		List<Teacher> list=jdbc.queryList(sql, new TeacherRowMapper());
		return list;
	}

	@Override
	public Teacher fetchById(int id) {
		String sql="select * from teacher where tid=?";
		List<Teacher> teacher=jdbc.queryList(sql, new TeacherRowMapper(), id);
		return teacher.size()>0?teacher.get(0):null;
	}

	@Override
	public Teacher fetchBy(String name) {
		String sql="select * from teacher where tname=?";
		List<Teacher> teacher=jdbc.queryList(sql, new TeacherRowMapper(), name);
		return teacher.size()>0?teacher.get(0):null;
	}

	@Override
	public boolean addTeacher(int tid, String tname, String tdescription, String timage) {
		ArrayList parameters = new ArrayList();
		parameters.add(tid);
		parameters.add(tname);
		parameters.add(tdescription);
		parameters.add(timage);
		String sql = "insert into teacher(tid,tname,tdescription,timage)values(?,?,?,?)";
		return jdbc.executeUpdate(sql, parameters.toArray());
	}

	@Override
	public boolean updateTeacher(int tid, String tname, String tdescription, String timage) {
		ArrayList parameters = new ArrayList();
		parameters.add(tid);
		parameters.add(tname);
		parameters.add(tdescription);
		parameters.add(timage);
		parameters.add(tname);
		String sql = "update teacher set tid=?,tname=?,tdescription=?,timage=? where tname=?";
		return jdbc.executeUpdate(sql, parameters.toArray());
	}

}
